//
//  ZyaPurchaseController.swift
//  SlaughterClient
//
//  Created by Zhouweibin on 2018/1/28.
//  Copyright © 2018年 zwb. All rights reserved.
//

import UIKit

class ZyaPurchaseController: BaseTableViewController {
    override func getSearchType() -> TableSearchType {
        return .hiddenx
    }
    var feedOrderList = [ZyaFeedOrderList]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 90
        tableView.separatorStyle = .none
        tableView.backgroundColor = Theme.tableViewColor
        tableView.register(ZyaPurchaseCell.self, forCellReuseIdentifier: ZyaPurchaseCell.identifier)
 
        tableView.mj_header?.beginRefreshing()
    }
    
    override func network() {
        KSNetwork.shared.doRequest(API.getOrders(page: page),showHud: false) { (data: BaseResult<ZyaFeedOrderList>) in
          
                if let infos = data.infoArrCombine {
                    self.tableView.mj_header!.endRefreshing()
                    self.tableView.mj_footer!.endRefreshing()
                    
                    if self.page == 1 {
                        self.feedOrderList.removeAll()
                     }
                     
                    self.feedOrderList += infos
                     self.tableView.reloadData()
               
                } else {
                    KSLog("无数据00")
                }
       
        }
    }
    
 
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return feedOrderList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: ZyaPurchaseCell.identifier) as? ZyaPurchaseCell else { fatalError() }
        cell.model = feedOrderList[indexPath.row]
        cell.deleteEvent = { [unowned self] in
            guard let id = self.feedOrderList[indexPath.row].idKey else {
                return
            }
            
            let alert = UIAlertController(title: "确认删除该单据", message: "", preferredStyle: .alert)
            let delete = UIAlertAction(title: "确定", style: .default) { (_) in
                
                KSNetwork.shared.doRequest(API.deleteOrder(idkey: id), successHandle: { (data: BaseResult<OperateResult>) in
                    KSToast(data.message, duration: 1.0, position: .center)
                    if data.message == "删除成功" {
                        self.tableView.beginUpdates()
                        self.feedOrderList.remove(at: indexPath.row)
                        self.tableView.deleteRows(at: [indexPath], with: .automatic)
                        self.tableView.endUpdates()
                        self.tableView.reloadData()
                    }
                })
                
            }
            
            let cancel = UIAlertAction(title: "取消", style: .cancel, handler: nil)
            alert.addAction(delete)
            alert.addAction(cancel)
            self.present(alert, animated: true, completion: nil)
        }
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let idkey = feedOrderList[indexPath.row].idKey {
            let vc = ZyaPurchaseDetailController()
            vc.refreshClosure = {
                self.tableView.mj_header?.beginRefreshing()
            }
            vc.main = self.feedOrderList[indexPath.row]
            vc.idkey = idkey
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
}


